/**
 * 读取zip文件中的文件内容
 */
import common from '../../tnxet-common.js';
import util from '../main-util.js';
import fs from 'fs';
import JSZip from 'jszip';

export default {
    name: common.event.zip.read,
    listen(zipPath, filename) {
        return new Promise((resolve, reject) => {
            if (zipPath) {
                try {
                    zipPath = util.file.getAbsolutePathBasedOnApp(zipPath);
                    let data = fs.readFileSync(zipPath);
                    JSZip.loadAsync(data).then(zip => {
                        let file = zip.file(filename);
                        if (file && !file.dir) {
                            file.async('string').then(content => {
                                resolve(content);
                            }).catch(error => {
                                reject(error);
                            });
                        }
                    }).catch(error => {
                        reject(error);
                    });
                } catch (error) {
                    reject(error);
                }
            } else {
                reject('zipPath is ' + zipPath);
            }
        });
    },
}
